package com.wjs.gulimall.gulimallware.service.impl;

import com.atguigu.common.constant.WareConstant;
import com.atguigu.common.vo.MergeVo;
import com.atguigu.common.vo.PurChaseDoneVo;
import com.atguigu.common.vo.PurchaseDoneItemVo;
import com.wjs.gulimall.gulimallware.entity.PurchaseDetailEntity;
import com.wjs.gulimall.gulimallware.service.PurchaseDetailService;
import com.wjs.gulimall.gulimallware.service.WareSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.wjs.gulimall.gulimallware.dao.PurchaseDao;
import com.wjs.gulimall.gulimallware.entity.PurchaseEntity;
import com.wjs.gulimall.gulimallware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Autowired
    private WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("status",1)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void merge(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();
        if(purchaseId==null){
            PurchaseEntity purchase=new PurchaseEntity();
            purchase.setCreateTime(new Date());
            purchase.setUpdateTime(new Date());
            purchase.setStatus(WareConstant.PurchaseType.CREATED.getCode());
            this.save(purchase);
            purchaseId=purchase.getId();
        }
        List<Long> items = mergeVo.getItems();
        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map((obj) -> {
            PurchaseDetailEntity entity = new PurchaseDetailEntity();
            entity.setPurchaseId(finalPurchaseId);
            entity.setId(obj);
            entity.setStatus(WareConstant.PurchaseDetailType.ASSIGEND.getCode());
            return entity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);

        PurchaseEntity purchase=new PurchaseEntity();
        purchase.setId(purchaseId);
        purchase.setUpdateTime(new Date());
        this.updateById(purchase);

    }

    @Override
    @Transactional
    public void purchaseRecevied(List<Long> ids) {
        List<PurchaseEntity> collect = ids.stream().map((obj) -> {
            PurchaseEntity byId = this.getById(obj);
            return byId;
        }).filter((obj) -> {
            return (obj.getStatus() == WareConstant.PurchaseType.CREATED.getCode() || obj.getStatus() == WareConstant.PurchaseType.ASSIGEND.getCode());
        }).map((obj) -> {
            obj.setStatus(WareConstant.PurchaseType.RECEIVE.getCode());
            obj.setUpdateTime(new Date());
            return obj;
        }).collect(Collectors.toList());

        //改变采购单的状态
        this.updateBatchById(collect);
        //改变采购项的状态
        collect.forEach((pur)->{
            List<PurchaseDetailEntity> detailEntities = purchaseDetailService.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", pur.getId()));
            List<PurchaseDetailEntity> collect1 = detailEntities.stream().map((obj) -> {
                PurchaseDetailEntity entity = new PurchaseDetailEntity();
                entity.setStatus(WareConstant.PurchaseDetailType.BUYING.getCode());
                entity.setId(obj.getId());
                return entity;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(collect1);
        });
    }

    @Override
    @Transactional
    public void purchaseDone(PurChaseDoneVo purChaseDoneVo) {
        Long id = purChaseDoneVo.getId();
        //改变采购项的状态
        Boolean flag=true;
        List<PurchaseDoneItemVo> items = purChaseDoneVo.getItems();
        List<PurchaseDetailEntity> updates=new ArrayList<>();
        for (PurchaseDoneItemVo item : items) {
            PurchaseDetailEntity entity = new PurchaseDetailEntity();
            if(item.getStatus()==WareConstant.PurchaseDetailType.HASERROR.getCode()){
                flag=false;
                entity.setStatus(WareConstant.PurchaseDetailType.HASERROR.getCode());
            }else {
                entity.setStatus(WareConstant.PurchaseDetailType.FINISH.getCode());
                //将成功采购的进行入库
                PurchaseDetailEntity detailEntity = purchaseDetailService.getById(item.getItemId());
                wareSkuService.addStock(detailEntity.getSkuId(),detailEntity.getWareId(),detailEntity.getSkuNum());

            }
            entity.setId(item.getItemId());
            updates.add(entity);
        }
        purchaseDetailService.updateBatchById(updates);

        //修改采购单状态
        PurchaseEntity purchase=new PurchaseEntity();
        purchase.setId(id);
        purchase.setStatus(flag?WareConstant.PurchaseType.FINISH.getCode() : WareConstant.PurchaseType.HASERROR.getCode());
        purchase.setUpdateTime(new Date());
        this.updateById(purchase);


    }

}